﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditBalanceFrm.aspx.cs"
    Inherits="WebApp.EditBalanceFrm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>收支费用资料</title>
    <link rel="stylesheet" type="text/css" href="css/newStyle.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/icon.css" />

    <script type="text/javascript" src="js/common.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>

    <script type="text/javascript" src="js/Jquery.Query.js"></script>

    <script type="text/javascript">
        var PageSize = 20;
        $(document).ready(function() {
            initParams();
        });

        function initParams() {
            var bType = $.query.get('BType');
            var balanceId = $.query.get('Id');
            initData();
            $('#eBType').combobox('setValue', bType);
            $('#editId').val(balanceId);
            bindBalance();
        }

        function bindBalance() {
            var id = $('#editId').val();
            if (id) {
                WebApp.EditBalanceFrm.GetBalance(id, function callback(p) {
                    if (p.error) {
                        $.messager.alert('异常消息', '获取数据异常,' + p.error.Message, 'error');
                        return;
                    }

                    var res = p.value;
                    if (res) {
                        var data = res.Data;
                        var msg = res.Msg;
                        if (msg)
                            $.messager.alert('收支信息获取异常', msg, 'error');
                        else {
                            if (data) {
                                $('#eBDate').datebox('setValue', DateFormatter(data.bDate));
                                $('#eBType').combobox('setValue', data.bType);
                                $('#eAmount').numberbox('setValue', data.amount);
                                $('#eTarget').combobox('setValue', data.target);
                                $('#eBank').val(data.bank);
                                $('#eDesc').val(data.desc);
                                $('#eMemo').val(data.memo);
                            }
                        }
                    }
                });
            }
        }

        function initData() {
            $('#eBDate').datebox('setValue', DateFormatter(new Date()));

            $.getJSON("JSONHandler.ashx?DataType=FeeType", function(result) {
                $('#eBType').combobox('loadData', result);
            });

            $.getJSON('JSONHandler.ashx?DataType=Client&AddKey=false', function(result) {
                $('#eTarget').combobox('loadData', result);
            });
        }

        function resetFrm() {
            $('#fm').form('clear');
        }

        function addBalance() {
            resetFrm();
        }

        function saveBalance() {
            var validate = $('#fm').form('validate');
            if (!validate)
                return;
            var id = $('#editId').val();
            var eBDate = $('#eBDate').datebox('getValue');
            var eBType = $('#eBType').combobox('getValue');
            var eAmount = $('#eAmount').numberbox('getValue');
            var eTarget = $('#eTarget').combobox('getValue');
            var eBank = $('#eBank').val();
            var eDesc = $('#eDesc').val();
            var eMemo = $('#eMemo').val();

            WebApp.EditBalanceFrm.SaveBalance(id, eBDate, eBType, eAmount, eTarget, eBank, eDesc, eMemo, function callback(p) {
                if (p.error) {
                    $.messager.alert('异常消息', '提交保存异常,' + p.error.Message, 'error');
                    return;
                }

                var res = p.value;
                if (res) {
                    var data = res.Data;
                    var msg = res.Msg;
                    if (msg)
                        $.messager.alert('收支信息获取异常', msg, 'error');
                    else {
                        refreshParent();
                        if (id) {
                            if (data > 0) {
                                $.messager.alert('消息', '收支信息已更新!', 'info');
                                $('#editId').val(data);
                            }
                            else
                                $.messager.alert('收支信息更新异常', msg, 'error');
                        }
                        else {
                            if (data > 0) {
                                $.messager.alert('消息', '收支信息已创建!', 'info');
                                $('#editId').val(data);
                            }
                            else
                                $.messager.alert('收支信息创建异常', msg, 'error');
                        }
                    }
                }
            });
        }

        function deleteBalance() {
            var id = $('#editId').val();
            $.messager.confirm('提示', '确认删除吗?', function(r) {
                if (r) {
                    WebApp.EditBalanceFrm.DeleteBalance(id, function callback(p) {

                        if (p.error) {
                            $.messager.alert('异常消息', '提交删除异常,' + p.error.Message, 'error');
                            return;
                        }
                        var res = p.value;
                        if (res) {
                            var data = res.Data;
                            var msg = res.Msg;
                            if (msg)
                                $.messager.alert('删除异常', msg, 'error');
                            else {
                                if (data == true) {
                                    $.messager.alert('消息', '收支信息已删除!', 'info');
                                    refreshParent();
                                }
                                else
                                    $.messager.alert('删除收支信息失败', msg, 'error');
                            }
                        }
                    });
                } 
            });
        }

        function refreshParent() {
            window.parent.doSearch(0);
        }
    </script>

</head>
<body>
    <form id="Form1" method="post" runat="server">
    </form>
    <div>
        <form id="fm">
        <div style="width: 500px; margin: 0 auto; overflow: auto; _display: inline-block;
            padding: 5px; border: 1px solid #ddd">
            <a href="#" id="btnAdd" class="easyui-linkbutton" data-options="plain:true" onclick="addBalance()">
                新增</a> <a href="#" id="btnSave" class="easyui-linkbutton" data-options="plain:true"
                    onclick="saveBalance()">保存</a> <a href="#" id="btnDelete" class="easyui-linkbutton"
                        data-options="plain:true" onclick="deleteBalance()">删除</a>
        </div>
        <div class="easyui-panel" title="收支资料" style="width: 500px; margin: 0 auto; overflow: auto;
            _display: inline-block; padding: 5px; text-align: center">
            <table style="text-align: center">
                <tr>
                    <td style="text-align: left">
                        日期：
                    </td>
                    <td style="text-align: left">
                        <input id="eBDate" class="easyui-datebox" data-options="formatter:DateFormatter,parser:DateParser"
                            style="width: 100px" required="true" />
                    </td>
                    <td style="text-align: left">
                        收支类型：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combobox" id="eBType" data-options="valueField:'Value',textField:'Name'"
                            style="width: 100px;" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        金额：
                    </td>
                    <td style="text-align: left">
                        <input type="text" id="eAmount" style="width: 150px" class="easyui-numberbox" data-options="min:0,precision:2"
                            required="true" />
                    </td>
                    <td style="text-align: left">
                        客户：
                    </td>
                    <td style="text-align: left">
                        <input class="easyui-combobox" id="eTarget" data-options="valueField:'id',textField:'fullName'"
                            style="width: 200px;">
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        银行：
                    </td>
                    <td style="text-align: left" colspan="3">
                        <input type="text" id="eBank" style="width: 250px" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        描述：
                    </td>
                    <td style="text-align: left" colspan="3">
                        <input type="text" id="eDesc" style="width: 250px" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                        备注：
                    </td>
                    <td style="text-align: left" colspan="3">
                        <input type="text" id="eMemo" style="width: 250px" />
                    </td>
                </tr>
            </table>
        </div>
        <input id="editId" type="hidden" />
        </form>
    </div>
</body>
</html>
